﻿using Dm.filter.log;
using WebApi.Commons.Helpers;
using WebApi.Models.Database.DbName;

namespace WebApi.Repository
{
    public class AccountLoginRepository : Repository<AccountLoginModel>, IScopedService
    {
        /// <summary>
        /// 根据账号id获取登录信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AccountLoginModel GetByAccountId(long id)
        {
            return GetFirst(p => p.AccountId == id);
        }

        /// <summary>
        /// 更新登录信息
        /// </summary>
        /// <param name="updateModel"></param>
        /// <returns></returns>
        public bool UpdateLoginInfo(AccountLoginModel updateModel)
        {
            return AsUpdateable(updateModel).UpdateColumns(p => new { p.Token, p.ExpireTime }).Where(p => p.Id == updateModel.Id).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public bool LoginOut(long accountId)
        {
            return AsUpdateable().SetColumns(p => p.Token == "").Where(p => p.AccountId == accountId).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 更新过期时间
        /// </summary>
        /// <param name="id"></param>
        /// <param name="expireTime"></param>
        /// <returns></returns>
        public bool UpdateExpireTime(long id, DateTime expireTime)
        {
            var sql = AsUpdateable().SetColumns(p => p.ExpireTime == expireTime).Where(p => p.Id == id);
            return sql.ExecuteCommand() > 0;
        }
    }
}
